<?php
class ControllerProductReviews extends Controller {
	
	private $error = array(); 
	
	public function index() {
		
		$this->language->load('product/reviews');
		$this->load->model('catalog/category');	
		$this->load->model('catalog/product');
		$this->load->model('catalog/review');
		$this->load->model('tool/image');
		
		$this->data['text_on'] = $this->language->get('text_on');
		$this->data['text_empty'] = $this->language->get('text_no_reviews');
	
		$this->data['breadcrumbs'] = array();
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link(),			
			'separator' => false
		);
		
		$this->data['breadcrumbs'][] = array(
			'text'      => ROUTE_REVIEWS_R,
			'href'      => $this->url->linkRewrite(ROUTE_REVIEWS_R),			
			'separator' => $this->language->get('text_separator')
		);
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else { 
			$page = 1;
		}	
		$limit = 24;
			
		if (isset($this->request->get['category_id'])){
			$categoryId = $this->request->get['category_id'];
			$categoryId_r = '_' . $categoryId;
		}else{
			$categoryId = '';
			$categoryId_r = '';
		}
		$category = '';
		$product = '';
		if ($categoryId) {
			$path = '';
			foreach (explode('_', $categoryId) as $path_id) {
				if (!$path) {
					$path = $path_id;
				} else {
					$path .= '_' . $path_id;
				}
				$category = $this->model_catalog_category->getCategory($path_id);
				if ($category) {
					$this->data['breadcrumbs'][] = array(
						'text'      => $category['name'],
						'href'      => $this->url->linkRewrite(ROUTE_REVIEWS_R, $category['name'] . '_c' . $path),
						'separator' => $this->language->get('text_separator')
					);
				}
			}
		}
		
		if (isset($this->request->get['product_id'])) {
			$product_id = $this->request->get['product_id'];
			$product = $this->model_catalog_product->getProduct($product_id);
		}
		
		$this->data['reviewList'] = array();
		
		if ($product) {
			$this->data['productPath'] = $this->url->linkRewrite('', $product['name'] . '_p' . $product_id . $categoryId_r);
			$this->data['breadcrumbs'][] = array(
				'text'      => $product['name'],
				'href'      => $this->url->linkRewrite(ROUTE_REVIEWS_R, $product['name'] . '_p' . $product_id . $categoryId_r),
				'separator' => $this->language->get('text_separator')
			);			
			
			$this->document->setTitle(ROUTE_REVIEWS_R . '-' . $product['name']);
			$this->document->setDescription(ROUTE_REVIEWS_R . '-' . $product['meta_description']);
			$this->document->setKeywords(ROUTE_REVIEWS_R . '-' . $product['meta_keyword']);
			
			$this->data['heading_title'] = ROUTE_REVIEWS_R . '-' . $product['name'];
			
			if ($product['image']) {
				$product['image'] = $this->model_tool_image->resize($product['image'], 100, 131);
			}
			
			$this->data['reviewsrating'] = $this->model_catalog_review->getGroupRating($product_id);
			if($product['reviews'] > 0){
				$results = $this->model_catalog_review->getReviewsByProductId($product_id, ($page-1)*$limit, $limit);
				foreach ($results as $result) {
		        	$this->data['reviewList'][] = array(
		        		'author'     => utf8_substr($result['author'], 0, 3) . '***',
						'text'       => $result['text'],
						'rating'     => (int)$result['rating'],
		        		'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
		        	);
		      	}
			}
			
			$pagination = new Pagination();
			$pagination->total = $product['reviews'];
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			if($categoryId){
				$pagination->url = $this->url->link('product/reviews', 'category_id=' . $categoryId . 'product_id=' . $product_id . '&page={page}');
			}else{
				$pagination->url = $this->url->link('product/reviews', 'product_id=' . $product_id . '&page={page}');
			}
			$this->data['pagination'] = $pagination->render();
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/reviews_info.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/product/reviews_info.tpl';
			} else {
				$this->template = 'default/template/product/reviews_info.tpl';
			}
			$this->data['product'] = $product;
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
						
			$this->response->setOutput($this->render());
		} else {
			if($category){
				$this->document->setTitle(ROUTE_REVIEWS_R . '-' . $category['name']);
				$this->document->setDescription(ROUTE_REVIEWS_R . '-' . $category['meta_description']);
				$this->document->setKeywords(ROUTE_REVIEWS_R . '-' . $category['meta_keyword']);
				$this->data['heading_title'] = ROUTE_REVIEWS_R . '-' . $category['name'];
				
				$reviewsNum = $this->model_catalog_review->getTotalReviewsByCategoryId($category['category_id']);
				$results = $this->model_catalog_review->getReviewsByCategoryId($category['category_id'], ($page-1)*$limit, $limit);
			}else{
				$this->document->setTitle(ROUTE_REVIEWS_R);
				$this->document->setDescription(ROUTE_REVIEWS_R);
				$this->document->setKeywords(ROUTE_REVIEWS_R);
				$this->data['heading_title'] = ROUTE_REVIEWS_R;
				
				$reviewsNum = $this->model_catalog_review->getTotalReviewsByCategoryId(0);
				$results = $this->model_catalog_review->getReviewsByCategoryId(0, ($page-1)*$limit, $limit);
			}
			if($results){
				foreach ($results as $result) {
		        	$this->data['reviewList'][] = array(
		        		'author'        => utf8_substr($result['author'], 0, 3) . '***',
						'text'          => utf8_substr($result['text'], 0, 50) . '...',
						'rating'        => $result['rating'],
						'date_added'    => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
						'image'         => $this->model_tool_image->resize($result['image'], 100, 131),
						'name'          => $result['name'],
						'href'    	    => $this->url->linkRewrite(ROUTE_REVIEWS_R, $result['name'] . '_p' . $result['product_id'] . $categoryId_r),
		        	);
		      	}
			}
			
			$pagination = new Pagination();
			$pagination->total = $reviewsNum;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			if($categoryId){
				$pagination->url = $this->url->link('product/reviews', 'category_id=' . $categoryId . '&page={page}');
			}else{
				$pagination->url = $this->url->link('product/reviews', '&page={page}');
			}
			$this->data['pagination'] = $pagination->render();
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/reviews_list.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/product/reviews_list.tpl';
			} else {
				$this->template = 'default/template/product/reviews_list.tpl';
			}
			
			$this->data['category'] = $category;
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
						
			$this->response->setOutput($this->render());
    	}
  	}
	
	public function review() {
    	$this->language->load('product/product');
		
		$this->load->model('catalog/review');

		$this->data['text_on'] = $this->language->get('text_on');
		$this->data['text_no_reviews'] = $this->language->get('text_no_reviews');

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}  
		
		$this->data['reviews'] = array();
		
		$review_total = $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['product_id']);
			
		$results = $this->model_catalog_review->getReviewsByProductId($this->request->get['product_id'], ($page - 1) * 10, 10);
      		
		foreach ($results as $result) {
        	$this->data['reviews'][] = array(
        		'author'     => utf8_substr($result['author'], 0, 3) . '***',
				'text'       => $result['text'],
				'rating'     => (int)$result['rating'],
        		'reviews'    => sprintf($this->language->get('text_reviews'), (int)$review_total),
        		'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
        	);
      	}			
			
		$pagination = new Pagination();
		$pagination->total = $review_total;
		$pagination->page = $page;
		$pagination->limit = 10; 
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('product/product/review', 'product_id=' . $this->request->get['product_id'] . '&page={page}');
			
		$this->data['pagination'] = $pagination->render();
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/review.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/product/review.tpl';
		} else {
			$this->template = 'default/template/product/review.tpl';
		}
		
		$this->response->setOutput($this->render());
	}
	
}
?>